package com.project_test.kun.travelling_1st.HomePage.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.project_test.kun.travelling_1st.GoodsDetailActivities.HouseDetail.House_Detail_Activity;
import com.project_test.kun.travelling_1st.HomePage.Util.URL;
import com.project_test.kun.travelling_1st.HomePage.adapter.HomePHouseListAdapter;
import com.project_test.kun.travelling_1st.HomePage.category.HomePHouseListItem;
import com.project_test.kun.travelling_1st.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HomePageHouseActivity extends AppCompatActivity implements View.OnClickListener {

    private RequestQueue mRequestQueue;
    private int cityId=143;//默认北京
    private int page=1;//默认加载第一页
    private List<HomePHouseListItem> mHomePHouseListItems;
    private HomePHouseListAdapter mAdapter;
    private String mUrl;
    private PullToRefreshListView mPListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page_house);
//        getSupportActionBar().hide();
        mRequestQueue = Volley.newRequestQueue(this);
        mHomePHouseListItems=new ArrayList<>();
        initView();
        //进入界面默认加载数据
        mUrl = URL.HOMEPAGER_HOUSE_COMMON+URL.HOUSE_CITY_ID+cityId+URL.HOUSE_PAGE+page+URL.HOMEPAGER_HOUSE_LASTCOMMON;
        initData(mUrl);
    }



    private void initData(String url) {
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray data = response.getJSONArray("data");
                            for (int i = 0; i < data.length(); i++) {
                                HomePHouseListItem houseListItem=new HomePHouseListItem();
                                houseListItem.pashJsonObject(data.getJSONObject(i));
                                mHomePHouseListItems.add(houseListItem);
                            }
                            mAdapter.notifyDataSetChanged();
                            Message msg = mHandler.obtainMessage();
                            msg.what=999;
                            mHandler.sendMessage(msg);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    mRequestQueue.add(jsonObjectRequest);
    }
private Handler mHandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what==999) {
            mPListView.onRefreshComplete();
        }
    }
};
    private void initView() {
        ImageView exit= (ImageView) findViewById(R.id.exit);
        exit.setOnClickListener(this);
        TextView toolText= (TextView) findViewById(R.id.homepage_house_cityText);
        mPListView = (PullToRefreshListView) findViewById(R.id.homepage_house_PlistView);
        mPListView.setMode(PullToRefreshBase.Mode.BOTH);//设置可上拉,下拉
        initHeadView();
        mAdapter = new HomePHouseListAdapter(this,mHomePHouseListItems);
        mPListView.setAdapter(mAdapter);
        mPListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=1;
                mUrl = URL.HOMEPAGER_HOUSE_COMMON+URL.HOUSE_CITY_ID+cityId+URL.HOUSE_PAGE+page+URL.HOMEPAGER_HOUSE_LASTCOMMON;
                mHomePHouseListItems.clear();
                initData(mUrl);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page+=page;
                mUrl = URL.HOMEPAGER_HOUSE_COMMON+URL.HOUSE_CITY_ID+cityId+URL.HOUSE_PAGE+page+URL.HOMEPAGER_HOUSE_LASTCOMMON;
                initData(mUrl);
            }
        });
    }

    private void initHeadView() {
        ListView refreshableView = mPListView.getRefreshableView();
        View headerView=View.inflate(this,R.layout.head_activity_homep_house_list,null);
        refreshableView.addHeaderView(headerView);
        LinearLayout inHome= (LinearLayout) headerView.findViewById(R.id.inHome_head_activity_hphouse);
        LinearLayout outHome= (LinearLayout) headerView.findViewById(R.id.outHome_head_activity_hphouse);
        inHome.setOnClickListener(this);
        outHome.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=null;
        if (view.getTag() instanceof Integer){
            int hid = (int) view.getTag();
            intent=new Intent(this,House_Detail_Activity.class);
            intent.putExtra("hid",hid);
            startActivity(intent);
        }else {
            switch (view.getId()) {
                case R.id.inHome_head_activity_hphouse:
//                    intent = new Intent(this, CalendarActivity.class);
//                    startActivity(intent);
                    break;
                case R.id.outHome_head_activity_hphouse:
//                    intent = new Intent(this, CalendarActivity.class);
//                    startActivity(intent);
                    break;
                case R.id.exit:
                    this.finish();
                    break;
            }
        }
    }

}
